<?php
/**
 * Address Professional
 * 
 */

use Hive\Ressource;
use Fox\Common\Address;
use Fox\Html\DropList;
use Fox\Html\Block;
use Fox\Html\Screen;
use Fox\Html\Input;

// Load Data
$xview =  Xview::instance();
/* @var $contact Hive\Ressource */
$contact = $xview->component('ressource', 'object');
// Customizing
$customizing = Hive\Ressource\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'ressource','act'=> 'addresses','sac'=>'professional'));
$companyData = array();
/* $currentJob Hive\Ressource\Experience */
$currentJob = $contact->currentPosition();

if($currentJob->exists()) {
	$company = $currentJob->company();
	if($company->exists()) {
		$companyData = $company->getData();
	}
}

$header = new Fox\Html\Header\Content('wish',Msg(Hive::BUNDLE,Hive::MENU_INFO_PRO));
$screen = new Screen('sInfoPro','update',null,true);

foreach($contact->addresses() as $addressLink) {
	if($addressLink->type == $contact::ADDRESS_PRO) {
		
		$tmpBk = $xview->component('ressource', 'addressPro');
		try {
			$data = $addressLink->address()->getData();
			$tmpBk->iname .= '__'.$addressLink->adrid;
			foreach($tmpBk->items() as $item) {
				$item->default = isset($data[$item->iname]) ? $data[$item->iname] : null;
				$item->iname .= '__'.$addressLink->adrid;
			}
			
			$adBlocks[$addressLink->adrid] = $tmpBk;
			$screen->add($tmpBk);
		}
		catch(Fox\Object\Exception\NotExist $e)
		{
			continue;
		}
	}
}
// Additionnal Address Block
if(!isset($adBlocks)) {
	$bkAddress = $xview->component('ressource', 'addressPro');
	$screen->add($bkAddress);
}
$blkComp = new Block('bAddressCompany',Msg(Hive::BUNDLE,Hive::TITLE_COMPANY_ADDRESS));
$blkComp->add(new Input(array('company','company_name')))->displayMode(Input::DISPLAY_READONLY);
$blkComp->add(new Input(array('adress','street'),false))->size(Input::SIZE_XLARGE);
$blkComp->add(new Input('postal_code',false));
$blkComp->add(new Input('city',false))->size(25);
$blkComp->add(new DropList('country',true,true,false,'fr'));
$blkComp->initialize($companyData);
$screen->add($blkComp)->displayMode(Input::DISPLAY_READONLY);
if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		if(isset($adBlocks)) {
			$displayMessage = false;
			foreach($adBlocks as $bkAddress) {
				list($bid,$adrid) = explode('__',$item->iname);
				foreach($bkAddress->items() as $item) {
					$id = explode('__',$item->iname);
					$data[$id[0]] = $item->get();	
				}

				try {
					$addressLink = $contact->findAddresses($contact->getInlineId().':'.$adrid);
					unset($data['adrid']);
					$addressLink->setFields($data);
					if($addressLink->update() ){
						$displayMessage = true;
					}
				} catch (Fox\Object\Exception\Save $e) {
					Fox\Message::Error($e->getMessage());
				} catch (\Exception $e) {
					Fox\Message::Error($e->getMessage());
				}
			}
			if(!$screen->errors()) {
				if($displayMessage) {
					\Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$contact->name()));
				} else {
					\Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NO_UPDATE,$contact->name()));
				}
				
			}
		} else {
			// New Address
			$addressData = $bkAddress->get();
			try {
				$address = new Address();
				$address->setFields($addressData);
				$address->create();
				$adrid = $address->getId();
				$addressLink = new Ressource\Address();
				$addressLink->setFields(array(
					'ressource_id' => $contact->getInlineId(),
					'adrid' => $adrid,
					'type' => $contact::ADDRESS_PRO
					)
				);
				$addressLink->create();
			} catch (Fox\Object\Exception\Save $e) {
				Fox\Message::Error($e->getMessage());
			}
		}
	}
	echo $xview->messages();
	exit;
}
?>
<div>
	<div class="page-header clearfix">
		<?php echo $header->render();?>
	</div>
	<div class="contentArea">
			<?php echo $screen->display();?>
	</div>
</div>